// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        console.log(this);

        /*(1)注册点击事件*/
        //on-注册,off-取消 
        this.node.on(cc.Node.EventType.TOUCH_START,this.on_touch_start,this);
        //this.node.off(cc.Node.EventType.TOUCH_START,this.on_touch_start,this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE,this.on_touch_move,this);
      
        this.node.on(cc.Node.EventType.TOUCH_END,this.on_touch_end,this);

        this.node.on(cc.Node.EventType.TOUCH_CANCEL,this.on_touch_cancel,this);
        //关闭所有点击注册
        //this.node.targetOff(this);

        /*(2)注册键盘事件*/
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,this.on_key_down,this);
        
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,this.on_key_up,this);

        /*(2)注册自定义事件*/
        //接受者
        this.node.on("event",this.event,this);

        //派发者，只能传给自己
        this.node.emit("event","msg");

        //派送者，不只发给自己，可以发给整个体系
        var newEvent=new cc.Event.EventCustom("event",true); // true-可向上(父节点)传递  false-不向上传递
        newEvent.detail={msg:"newMsg"};
        this.node.dispatchEvent(newEvent);
        
    },
    event: function(e){
        console.log(e);
        
    },
    on_key_down: function(t){
        console.log("cc.SystemEvent.EventType.KEY_DOWN");
        console.log(t.keyCode);
        switch (t.keyCode)
        {
            case 37:
                this.node.x-=1;
                break;
            case 38:
                this.node.y+=1;
                break;  
            case 39:
                this.node.x+=1;
                break;
            case 40:
                this.node.y-=1;
                break;
        }
    },
    on_key_up: function(t){
        console.log("cc.SystemEvent.EventType.KEY_DOWN");
        
    },
    on_touch_start:function(t){
        console.log("cc.Node.EventType.TOUCH_START");
        //获取点击的坐标，坐标以【屏幕】左下角为原点
        var location=t.getLocation();
        console.log("touchLocation:",location," x:",location.x," y:",location.y);

        this.node.setPosition(cc.v2(this.node.getPosition().x+2,this.node.getPosition().y+0));
        this.node.setScale(0.5);
    },
    on_touch_move: function(t)
    {
        console.log("cc.Node.EventType.TOUCH_MOVEED");
        var delta=t.getDelta();
        this.node.x+=delta.x
        this.node.y+=delta.y
    },
    on_touch_end: function(t)
    {
        console.log("cc.Node.EventType.TOUCH_CANCEL");
        this.node.setScale(1.0);
    },
    on_touch_cancel: function(t)
    {
        console.log("cc.Node.EventType.TOUCH_CANCEL");
        this.node.setScale(1.0);
    },
    

    start () {

    },

    // update (dt) {},
});
